package com.sidianzhong.sdz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class WebEquipmentonoffrecordServiceImpl implements WebEquipmentonoffrecordService{
    protected static final Logger LOG = LoggerFactory.getLogger(WebEquipmentonoffrecordServiceImpl.class);

    // 将所有的modelMapper注入
    @Autowired
    private WebEquipmentonoffrecordMapper webEquipmentonoffrecordMapper;


    @Override
    public WebEquipmentonoffrecord create(WebEquipmentonoffrecord item) {

         webEquipmentonoffrecordMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return webEquipmentonoffrecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(WebEquipmentonoffrecord item) {

        return webEquipmentonoffrecordMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public WebEquipmentonoffrecord get(Integer id) {
        return webEquipmentonoffrecordMapper.selectByPrimaryKey(id);
    }
    @Override
     public PageInfo<WebEquipmentonoffrecord> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,WebEquipmentonoffrecord item){

        WebEquipmentonoffrecordExample  example = new WebEquipmentonoffrecordExample();
        WebEquipmentonoffrecordExample.Criteria criteria = example.createCriteria();
        if(item.getEquipmenttypekey() != null ){
            criteria.andEquipmenttypekeyEqualTo(item.getEquipmenttypekey());
        }
        if(item.getEquipmentkey() != null ){
            criteria.andEquipmentkeyEqualTo(item.getEquipmentkey());
        }
        if( item.getTurnonofftime() != null ){
//            criteria.andTurnonofftimeEqualTo(Tools.convertStringToDate(item.getTurnonofftime(), "yyyy-MM-dd HH:mm:ss"));
        }
        if(item.getOperatingtype() != null ){
            criteria.andOperatingtypeEqualTo(item.getOperatingtype());
        }
        if( item.getRecordtime() != null ){
//            criteria.andRecordtimeEqualTo(Tools.convertStringToDate(item.getRecordtime(), "yyyy-MM-dd HH:mm:ss"));
        }
        if(item.getStaffname() != null ){
            criteria.andStaffnameEqualTo(item.getStaffname());
        }

        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List< WebEquipmentonoffrecord> list = this.webEquipmentonoffrecordMapper.selectByExample(example);
        PageInfo result = new PageInfo(list);

        return result;

     }

    @Override
    public JSONObject setlectTime(String equipmenttypekey, String equipmentkey) {
        JSONObject jsonObject = new JSONObject();
        WebEquipmentonoffrecord webEquipmentonoffrecord = this.webEquipmentonoffrecordMapper.setlectTime(equipmenttypekey, equipmentkey, 1);
        if (null!=webEquipmentonoffrecord){
            jsonObject.put("open",webEquipmentonoffrecord.getTurnonofftime().getTime());
        }else {
            jsonObject.put("open",new Date().getTime());
        }

        WebEquipmentonoffrecord webEquipmentonoffrecord1 = this.webEquipmentonoffrecordMapper.setlectTime(equipmenttypekey, equipmentkey, 2);
        if (null!=webEquipmentonoffrecord){
            jsonObject.put("close",webEquipmentonoffrecord1.getTurnonofftime().getTime());
        }else {
            jsonObject.put("close",new Date().getTime());
        }
        return jsonObject;
    }

    @Override
    public int batchInsert(List<WebEquipmentonoffrecord> list) {
        return this.webEquipmentonoffrecordMapper.batchInsert(list);
    }


}
